package hero.mps.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import hero.mps.constant.MessageConstant;
import hero.mps.constant.RedisConstant;
import hero.mps.entity.PageResult;
import hero.mps.entity.QueryPageBean;
import hero.mps.entity.Result;
import hero.mps.pojo.OrderSetting;
import hero.mps.service.OrderSettingService;
import hero.mps.utils.POIUtils;
import hero.mps.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 体检检查项管理
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    //新增
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            //获取Excel文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list !=null && list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    String orderDate = strings[0];
                    String number = strings[1];
                    OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                    orderSettingList.add(orderSetting);
                }
                orderSettingService.add(orderSettingList);
            }

            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            //图片上传失败
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){ //参数格式 2023-11
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){ //参数格式 2023-11
        try {
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}